package com.yjhb.android.sbz.v2.fragment.lastrank

import com.alibaba.fastjson.JSONArray
import com.alibaba.fastjson.JSONObject
import com.yjhb.android.sbz.R
import com.yjhb.android.sbz.tool.LoadingDialogTool
import com.yjhb.android.sbz.v2.adapter.V2GameRewardRankAdapter
import com.yjhb.android.sbz.v2.entity.*
import com.yjhb.android.sbz.v2.mvp.presenter.ConfigPresenter
import com.yjhb.android.sbz.v2.mvp.presenter.V2LastRankPresenter
import com.yjhb.android.sbz.v2.mvp.view.ConfigView
import com.yjhb.android.sbz.v2.mvp.view.V2LastRankView
import com.jojo.android.zxlib.base.BaseFragment
import com.jojo.android.zxlib.view.dialog.ILoading
import kotlinx.android.synthetic.main.fragment_game_reward_last_rank_v2.*
import kotlinx.android.synthetic.main.fragment_game_reward_last_rank_v2.emptyRecyclerView
import kotlinx.android.synthetic.main.fragment_game_reward_last_rank_v2.emptyView
import kotlinx.android.synthetic.main.fragment_game_reward_last_rank_v2.num_tv


/**
 *  创建者 zx
 *  创建时间 2021/4/26 15:27
 */
class V2GameRewardLastRankFragment : BaseFragment<V2LastRankPresenter<GameRewardItem>>(), V2LastRankView<GameRewardItem>, ConfigView {

    private var adapterV2: V2GameRewardRankAdapter? = null
    private val data = mutableListOf<GameRewardItem>()
    private var loadingView: ILoading? = null

    private val configPresenter = lazy { ConfigPresenter(this) }

    override fun initPresenter() = V2LastRankPresenter(this)

    override fun getLayoutId() = R.layout.fragment_game_reward_last_rank_v2

    override fun initUI() {
        //设置空视图
        emptyView?.showLoading()
        emptyRecyclerView?.emptyView = emptyView
        activity?.run {
            adapterV2 = V2GameRewardRankAdapter(this)
            adapterV2
        }?.let {
            it.data = this.data
        }
        emptyRecyclerView?.adapter = this.adapterV2
        loadingView = LoadingDialogTool.createDialog(this)

        configPresenter.value.getSysConfig("OldGameReward")
    }

    override fun initEvent() {

    }

    override fun initData() {
        presenter.getLastRank(3, GameRewardItem::class.java)
    }

    override fun onConfig(config: JSONObject?) {
        config?.let {
            colRank_tv?.text = it.getString("colRank")
            colMoney_tv?.text = it.getString("colMoney")
            colReward_tv?.text = it.getString("colReward")
        }
    }

    override fun onConfig(config: JSONArray?) {

    }

    override fun onFailure(code: Int, msg: String?) {
        emptyView?.showFail(msg, true)
    }


    override fun onLastRank(rank: LastRank<GameRewardItem>) {
        data.clear()
        rank.rankAgent?.let {
            data.addAll(it)
            adapterV2?.notifyDataSetChanged()
        }
        num_tv?.text = rank.period
        emptyView?.showTips("暂无上期数据")
    }

    override fun onLoading() {
        emptyView?.showLoading()
    }
}